﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Taobao.EF;
using Taobao.Models;

namespace Taobao.Manger
{
    /// <summary>
    /// 数据库业务
    /// </summary>
    public class DBManager
    {
        DbEntity db = null;
        public DBManager()
        {
            //实例化ef
            db = new DbEntity();
        }
        /// <summary>
        /// 充值  套餐类型
        /// </summary>
        /// <param name="type"> 1 2 3 </param>
        /// <returns></returns>
        public bool Charge(int type, int userId)
        {
            double money = 0;
            int buyTimes = 0;
            int sendTimes = 0;

            if (type == (int)ChargeEnum.套餐一)
            {
                money = 9.8;
                buyTimes = 278;
            }
            else if (type == (int)ChargeEnum.套餐二)
            {
                money = 98;
                buyTimes = 3333;
                sendTimes = 167;
            }
            else if (type == (int)ChargeEnum.套餐三)
            {
                money = 298;
                buyTimes = 10000;
                sendTimes = 833;
            }
            else if (type == (int)ChargeEnum.首充)
            {

            }
            //插入充值详细表
            string orderId = "Y" + DateTime.UtcNow.ToString("yyyyMMddHHmmssfff");
            ChangeDetail cd = new ChangeDetail();
            cd.orderId = orderId;
            cd.Num = buyTimes;
            cd.RMB = money;
            cd.SongCount = sendTimes;
            cd.UserId = userId;
            cd.ChangeTime = DateTime.Now;



            //更新主表信息
            UseInfor userInfo = new UseInfor
            {
                UserId = userId
            };
            var us = db.UseInfors.Attach(userInfo);

            us.SongCount += sendTimes;
            us.BuyCount = us.BuyCount + buyTimes;
            var tran = db.Database.BeginTransaction();//开启事务
            try
            {
                db.ChangeDetails.Add(cd);
                db.SaveChanges();
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                return false;
            }
            return true;
        }
        /// <summary>
        /// 消费
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public bool Cost(int userId, int count, List<string> proIds, UseInfor userInfo)
        {
            //首先消费购买的流量 再消费赠送的流量   
            int total = (int)userInfo.BuyCount;//购买的流量
            int send = (int)userInfo.SongCount;
            int costbuy = 0;//消费买的流量
            int costsend = 0;//消费赠送的流量
            if (total >= count)
            {
                costbuy = count;
                total = total - count;
            }
            else
            {
                costbuy = total;
                costsend = count - total;
                total = 0;
                send = total + send - count;
            }
            userInfo.SongCount = send;
            userInfo.BuyCount = total;
            //增加消费记录
            for (int index = 0; index < proIds.Count; index++)
            {
                var proId = proIds[index];
                CostDetail costd = new CostDetail();
                costd.productId = proId;
                costd.UserId = userId;
                costd.costSend = costsend;
                costd.costbuy = costbuy;
                costd.costTime = DateTime.Now;
                db.CostDetails.Add(costd);
            }
            var tran = db.Database.BeginTransaction();//开启事务

            try
            {

                db.SaveChanges();
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();

                return false;
            }
            //if(userId)
            return true;
        }
        /// <summary>
        /// 校验消费个数是否大于剩余个数
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="costCount"></param>
        /// <returns></returns>
        public bool CheckCount(int userId, int costCount, ref UseInfor userInfo)
        {
            userInfo = db.UseInfors.Where(o => o.UserId == userId).FirstOrDefault();
            int total = (int)userInfo.BuyCount;//购买的流量
            int send = (int)userInfo.SongCount;

            if (total + send < costCount)
            {
                return false;
            }
            return true;
        }
         
    }
}